<template>
  <div>
    <v-back></v-back>

    <van-address-edit
      :area-list="areaList"
      :address-info="resslist"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
    >
      <van-radio-group v-model="radio" direction="horizontal">
        <van-radio name="家">家</van-radio>
        <van-radio name="公司">公司</van-radio>
        <van-radio name="学校">学校</van-radio>
      </van-radio-group>
    </van-address-edit>
  </div>
</template>


<script>
import {
  addressadd,
  addressedit,
  addressremove,
  addresslist,
} from "../request/app.js";
import { areaList } from "@vant/area-data";
export default {
  data() {
    return {
      areaList,
      radio: "家",
      resslist:{}
    };
  },
  methods: {
    onSave(content) {
      // addressDetail: "五方桥";
      // areaCode: "130102";
      // city: "石家庄市";
      // country: "";
      // county: "长安区";
      // isDefault: false;
      // name: "李";
      // postalCode: "233600";
      // province: "河北省";
      // tel: "13333333333";
      // 这是新增地址
      if (this.$route.query.type == 1) {
        addressadd({
          uid: JSON.parse(localStorage.getItem("info")).uid,
          username: content.name,
          userphone: content.tel,
          regioncode: content.areaCode,
          location: content.province + content.city + content.county,
          useraddress: content.addressDetail,
          status: content.isDefault ? 1 : 0,
          label: this.radio,
        }).then((val) => {
          if (val.code != 200) {
            this.$toast(val.msg);
            return;
          }
          this.$toast(val.msg);

          // 跳转到地址管理
          this.$router.back();
        });
      } else {
        addressedit({
          id: this.$route.query.id,
          username: content.name,
          userphone: content.tel,
          regioncode: content.areaCode,
          location: content.province + content.city + content.county,
          useraddress: content.addressDetail,
          status: content.isDefault ? 1 : 0,
          label: this.radio,
        }).then((val) => {
          this.$toast(val.msg);

          // 跳转到地址管理
          this.$router.back();
        });
      }
    },
    onDelete() {
      addressremove({
        id: this.$route.query.id,
      }).then((val) => {
        if (val.code != 200) {
          this.$toast(val.msg);
          return;
        }

        // 跳转到地址管理
        this.$router.back();
      });
    },
  },
  mounted() {
    addresslist({
      uid: JSON.parse(localStorage.getItem("info")).uid,
      id: this.$route.query.id,
    }).then((val) => {
      if (val.code != 200) {
        return;
      }
      if (this.$route.query.type == 2) {
        this.resslist = {
          id: val.list[0].id,
          name: val.list[0].username,
          tel: val.list[0].userphone,
          // province:res.list[0].userphone,
          // city
          // county
          addressDetail: val.list[0].useraddress,
          areaCode: val.list[0].regioncode, //地区码可以识别地区

          isDefault: val.list[0].status ? true : false,
        };
      }
    });
  },
};
</script>


<style scoped>
</style>